package cn.openea.perpartner.auth.controller;


import cn.hutool.core.util.IdUtil;
import cn.openea.perpartner.common.auth.entity.LoginBody;
import cn.openea.perpartner.common.auth.login.LoginService;
import cn.openea.perpartner.common.auth.util.SessionUtil;
import cn.openea.perpartner.common.core.constant.Constants;
import cn.openea.perpartner.common.core.manager.AsyncManager;
import cn.openea.perpartner.common.core.utils.MessageUtils;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.log.factory.AsyncFactory;
import cn.openea.perpartner.system.entity.SysUser;
import cn.openea.perpartner.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "App认证")
@RestController
@RequestMapping("/app")
public class AppController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private ISysUserService userService;
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "账号", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    @PostMapping("/login")
    public AjaxResult login(String userId, String password) {
        AjaxResult ajaxResult  = userService.checkUser(userId, password);
        SysUser user = (SysUser) ajaxResult.get(AjaxResult.DATA_TAG);
        if (ajaxResult.get(AjaxResult.CODE_TAG).equals(AjaxResult.Type.SUCCESS.value())) {
            LoginBody loginBody = new LoginBody();
            loginBody.setUserid(user.getUserid());
            loginBody.setUsername(user.getNickname());
            loginBody.setVersion(IdUtil.fastSimpleUUID());
            loginBody.setExpireMinute(loginService.getRedisExpireMinute());
            loginBody.setExpireFreshTime(System.currentTimeMillis());
            String sessionId = SessionUtil.makeSessionId(loginBody);
            loginService.login(sessionId, loginBody);
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(userId, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
            return new AjaxResult(AjaxResult.Type.SUCCESS, "user.login.success",sessionId);
        }
        return AjaxResult.error((String) ajaxResult.get(AjaxResult.MSG_TAG));
    }


    @ApiOperation("用户注销")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionId", value = "sessionId", required = true)
    })
    @PostMapping("/logout")
    public AjaxResult logout(String sessionId) {
        loginService.logout(sessionId);
        return AjaxResult.success();
    }

    @ApiOperation("登录校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionId", value = "sessionId", required = true)
    })
    @PostMapping("/logincheck")
    public AjaxResult loginCheck(String sessionId) {
        LoginBody user = loginService.loginCheck(sessionId);
        if (user == null) {
            return new AjaxResult(AjaxResult.Type.ERROR, "sso not login.");
        }
        return new AjaxResult(AjaxResult.Type.SUCCESS, "", user);
    }
}
